package com.lkk.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;
import com.base.Constant;
import com.lkk.util.CompressUtil;
import com.lkk.util.EncryptUtil;
import com.lkk.util.ServiceConstant;
/**
 * 加密解密类
 * @author admin
 *
 */
public class InterfaceUtil {

	/**
	 * 解密解压
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String preHandler(HttpServletRequest request) throws UnsupportedEncodingException {
		byte[] postBytes = null;
		try {
			postBytes = generateByteArray(request);
		} catch (IOException e) {

		}

		if (postBytes == null || postBytes.length == 0) {

		}
		// 先解密
		byte[] unDecryptBytes = null;
		try {
			unDecryptBytes = EncryptUtil.decryptDES(postBytes, ServiceConstant.ENCRYPTED_PASSWORD);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(new String(unDecryptBytes));
		// 再解压缩
		byte[] unCompressBytes = null;
		try {
			unCompressBytes = CompressUtil.uncompressByGzip(unDecryptBytes, Constant.UTF8);
			if (unCompressBytes == null) {
				return "{}";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return new String(unCompressBytes, Constant.UTF8);
	}

	public static String preHandler(byte[] postBytes) throws UnsupportedEncodingException {
		// 先解密
		byte[] unDecryptBytes = null;
		try {
			unDecryptBytes = EncryptUtil.decryptDES(postBytes, ServiceConstant.ENCRYPTED_PASSWORD);
		} catch (Exception e) {

		}
		// 再解压缩
		byte[] unCompressBytes = null;
		try {
			unCompressBytes = CompressUtil.uncompressByGzip(unDecryptBytes, Constant.UTF8);
		} catch (IOException e) {

		}

		return new String(unCompressBytes, Constant.UTF8);
	}

	public static byte[] generateByteArray(HttpServletRequest request) throws IOException {
		BufferedReader read = null;
		byte[] inputBytes = null;
		try {
			read = request.getReader();
			inputBytes = IOUtils.toByteArray(read, Constant.UTF8);
		} finally {
			if (read != null) {
				read.close();
			}
		}

		return inputBytes;
	}

	/**
	 * 加密加压
	 * 
	 * @param response
	 * @return
	 */
	public static byte[] finalResponseHander(Object response) {
		// 原始返回包信息
		String responseStr = JSON.toJSONString(response);

		byte[] encrypt = null;
		try {
			byte[] origion = responseStr.getBytes(Constant.UTF8);
			// 压缩
			byte[] conpress = CompressUtil.compressByGzip(origion);
			// 加密
			encrypt = EncryptUtil.encryptDES(conpress, ServiceConstant.ENCRYPTED_PASSWORD);
		} catch (UnsupportedEncodingException e) {

			return JSON.toJSONString(response).getBytes();
		} catch (Exception e) {

			return JSON.toJSONString(response).getBytes();
		}

		return encrypt;
	}

	public static byte[] finalResponseHander(byte[] origion) {
		byte[] encrypt = null;
		try {
			// 压缩
			byte[] conpress = CompressUtil.compressByGzip(origion);
			// 加密
			encrypt = EncryptUtil.encryptDES(conpress, ServiceConstant.ENCRYPTED_PASSWORD);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encrypt;
	}

	public static String preHandler(ServletRequest requestWrapper) throws UnsupportedEncodingException {
		byte[] postBytes = null;
		try {
			postBytes = generateByteArray(requestWrapper);
		} catch (IOException e) {

		}

		if (postBytes == null || postBytes.length == 0) {

		}
		// 先解密
		byte[] unDecryptBytes = null;
		try {
			unDecryptBytes = EncryptUtil.decryptDES(postBytes, ServiceConstant.ENCRYPTED_PASSWORD);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(new String(unDecryptBytes));
		// 再解压缩
		byte[] unCompressBytes = null;
		try {
			unCompressBytes = CompressUtil.uncompressByGzip(unDecryptBytes, Constant.UTF8);
			if (unCompressBytes == null) {
				return "{}";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return new String(unCompressBytes, Constant.UTF8);
	}

	private static byte[] generateByteArray(ServletRequest requestWrapper) throws IOException {
		BufferedReader read = null;
		byte[] inputBytes = null;
		try {
			read = requestWrapper.getReader();
			inputBytes = IOUtils.toByteArray(read, Constant.UTF8);
		} finally {
			if (read != null) {
				read.close();
			}
		}

		return inputBytes;
	}
}
